|
1
|
|
|
import mkdirp from 'mkdirp' |
|
2
|
|
|
import { |
|
3
|
|
|
Util, |
|
|
|
|
|
|
4
|
|
|
cmsData, |
|
5
|
|
|
FileParser, |
|
6
|
|
|
fileUtils, |
|
7
|
|
|
config, |
|
8
|
|
|
Page, |
|
9
|
|
|
cmsTemplate, |
|
10
|
|
|
cleanSlug |
|
11
|
|
|
} from '../../cli' |
|
12
|
|
|
|
|
13
|
|
|
var page = function (req, res, next) { |
|
|
|
|
|
|
14
|
|
|
var filePath = cleanSlug(req.query.filePath) |
|
15
|
|
|
filePath = fileUtils.getFilePath(filePath) |
|
16
|
|
|
var html = (req.query.html) ? true : false |
|
17
|
|
|
var json = null |
|
18
|
|
|
var editor = false |
|
19
|
|
|
if(typeof req.body.json !== 'undefined' && req.body.json !== null) { |
|
20
|
|
|
editor = true |
|
21
|
|
|
if(typeof req.body.json === 'string') { |
|
22
|
|
|
json = JSON.parse(req.body.json) |
|
23
|
|
|
}else { |
|
24
|
|
|
json = req.body.json |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if(typeof filePath !== 'undefined' && filePath !== null) { |
|
29
|
|
|
var jsonPath = null |
|
30
|
|
|
var linkPath = null |
|
31
|
|
|
|
|
32
|
|
|
var filePathTest = cmsData.revision.getDocumentRevision(req.query.filePath) |
|
33
|
|
|
if(typeof filePathTest !== 'undefined' && filePathTest !== null) { |
|
34
|
|
|
// filePath = filePathTest.path |
|
35
|
|
|
jsonPath = filePathTest.path |
|
36
|
|
|
linkPath = filePathTest.abe_meta.link |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if(jsonPath === null || !fileUtils.isFile(jsonPath)) { |
|
40
|
|
|
res.status(404).send('Not found') |
|
41
|
|
|
return |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if(typeof json === 'undefined' || json === null) { |
|
45
|
|
|
json = FileParser.getJson(jsonPath) |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
let meta = config.meta.name |
|
49
|
|
|
|
|
50
|
|
|
var template = '' |
|
51
|
|
|
if(typeof json[meta] !== 'undefined' && json[meta] !== null && json[meta] !== '' |
|
52
|
|
|
&& json[meta].template !== 'undefined' && json[meta].template !== null && json[meta].template !== '') { |
|
53
|
|
|
template = json[meta].template |
|
54
|
|
|
}else { |
|
55
|
|
|
template = req.params[0] |
|
56
|
|
|
} |
|
57
|
|
|
var text = cmsTemplate.template.getTemplate(template) |
|
58
|
|
|
|
|
59
|
|
|
if (!editor) { |
|
60
|
|
|
|
|
61
|
|
|
cmsData.source.getDataList(fileUtils.removeLast(linkPath), text, json) |
|
62
|
|
|
.then(() => { |
|
63
|
|
|
var page = new Page(template, text, json, html) |
|
64
|
|
|
res.set('Content-Type', 'text/html') |
|
65
|
|
|
res.send(page.html) |
|
66
|
|
|
}).catch(function(e) { |
|
67
|
|
|
console.error(e) |
|
68
|
|
|
}) |
|
69
|
|
|
}else { |
|
70
|
|
|
text = cmsData.source.removeDataList(text) |
|
71
|
|
|
var page = new Page(template, text, json, html) |
|
72
|
|
|
res.set('Content-Type', 'text/html') |
|
73
|
|
|
res.send(page.html) |
|
74
|
|
|
} |
|
75
|
|
|
}else { |
|
76
|
|
|
// not 404 page if tag abe image upload into each block |
|
77
|
|
|
if(/upload%20image/g.test(req.url)) { |
|
78
|
|
|
var b64str = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' |
|
79
|
|
|
var img = new Buffer(b64str, 'base64') |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
res.writeHead(200, { |
|
82
|
|
|
'Content-Type': 'image/jpeg', |
|
83
|
|
|
'Content-Length': img.length |
|
84
|
|
|
}) |
|
85
|
|
|
res.end(img) |
|
86
|
|
|
}else { |
|
87
|
|
|
res.status(404).send('Not found') |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
export default page |